package com.ruoyi.warn.service.impl;

import com.ruoyi.base.domain.BusWarnWarninfo;
import com.ruoyi.base.domain.BusWaterSegment;
import com.ruoyi.base.mapper.BusWarnWarninfoMapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.warn.domain.PsCodPollutant;
import com.ruoyi.warn.domain.WarnInfoVo;
import com.ruoyi.warn.mapper.WarnDataMapper;
import com.ruoyi.warn.service.WarnDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class WarnDataServiceImpl implements WarnDataService {
    @Autowired
    private WarnDataMapper warnDataMapper;

    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private BusWarnWarninfoMapper warninfoMapper;

    @Override
    public List<BusWarnWarninfo> getSegmentWarnData(WarnInfoVo vo) {
//判断是否手工站
        if ("IsAuto".equals(vo.getSegmentType())) {
            //所有的自动站
            if (StringUtils.isNotEmpty(vo.getSegmentId())) {
                vo.setSegmentIdList(Arrays.asList(vo.getSegmentId().split(",")));
            } else {
                //获取自动站信息
                BusWaterSegment inVo = new BusWaterSegment();
                inVo.setIsauto("1");
                inVo.setIsdrink("0");
                List<BusWaterSegment> segmentDataList = warnDataMapper.getSegmentData(inVo);
                List<String> segmentList = segmentDataList.stream().map(BusWaterSegment::getId).collect(Collectors.toList());
                vo.setSegmentIdList(segmentList);
            }

        } else if ("IsManual".equals(vo.getSegmentType())) {
            //所有的手工站id
            if (StringUtils.isNotEmpty(vo.getSegmentId())) {
                vo.setSegmentIdList(Arrays.asList(vo.getSegmentId().split(",")));
            } else {
                //获取自动站信息
                BusWaterSegment inVo = new BusWaterSegment();
                inVo.setIsmanual("1");
                inVo.setIsdrink("0");
                List<BusWaterSegment> segmentDataList = warnDataMapper.getSegmentData(inVo);
                List<String> segmentList = segmentDataList.stream().map(BusWaterSegment::getId).collect(Collectors.toList());
                vo.setSegmentIdList(segmentList);
            }
        }
        if (StringUtils.isNotEmpty(vo.getItemCode())) {
            vo.setItemCodeList(Arrays.asList(vo.getItemCode().split(",")));
        }
        if (StringUtils.isNotEmpty(vo.getDataType())) {
            vo.setDataTypeList(Arrays.asList(vo.getDataType().split(",")));
        }
        if ("1".equals(vo.getDateType())) {//月
            vo.setBeginTime(vo.getBeginTime() + "-01");
            vo.setEndTime(DateUtils.getLastDayOfMonth(DateUtils.dateTime("yyyy-MM", vo.getEndTime())) + " 23:59:59");
        } else if ("2".equals(vo.getDateType())) {//年
            //判断是否是本年，如果是本年只查询到本月底
            int year = DateUtils.getYear(DateUtils.dateTime(), "yyyyMMdd");
            if (year == Integer.parseInt(vo.getEndTime())) {
                vo.setBeginTime(DateUtils.dateFormat(DateUtils.getFirstDayOfYear(Integer.parseInt(vo.getBeginTime())), "yyyy-MM-dd"));
                String lastDayOfYear = DateUtils.getLastDayOfMonth(new Date());
                vo.setEndTime(lastDayOfYear + " 23:59:59");
            } else {
                vo.setBeginTime(DateUtils.dateFormat(DateUtils.getFirstDayOfYear(Integer.parseInt(vo.getBeginTime())), "yyyy-MM-dd"));
                Date lastDayOfYear = DateUtils.getLastDayOfYear(Integer.parseInt(vo.getEndTime().trim().substring(0, 4)));
                vo.setEndTime(DateUtils.dateTime(lastDayOfYear) + " 23:59:59");
            }
        } else {//日
            if (StringUtils.isNotEmpty(vo.getEndTime())) {
                vo.setEndTime(vo.getEndTime() + " 23:59:59");
            }
        }
        //如果预警类型为空时查询全部
        List<BusWarnWarninfo> warnDataInfoList = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getWarnType())) {
                List<BusWarnWarninfo> warnDataList = warnDataMapper.getWarnDataInfo(vo);
                warnDataInfoList.addAll(warnDataList);
        } else {
            List<BusWarnWarninfo> warnDataList = warnDataMapper.getWarnDataInfo(vo);
            warnDataInfoList.addAll(warnDataList);
        }

        //预警类型数据
        List<Map<String, String>> warnTypeDataList = getWarnTypeData();

        for (BusWarnWarninfo warnData : warnDataInfoList) {
            String segmentId = warnData.getSegmentid();//断面id
            String itemCode = warnData.getItemid();//因子id
            String warnType = warnData.getWarntype();//报警类型
            String warnTime = warnData.getWarntime();

            if (StringUtils.isNotEmpty(warnTime)) {
                warnData.setSampleDate(DateUtils.dateFormat(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", warnData.getWarntime()), "yyyy-MM-dd HH"));
            }
            //报警级别
            if (StringUtils.isNotEmpty(warnData.getWarnlevel())) {
//                List<Map> levelList= itemCodelist.stream().filter(s->s.get("code").equals(warnData.getWarnLevel())).collect(Collectors.toList());
                if ("1".equals(warnData.getWarnlevel())) {
                    warnData.setWarnLevelName("一级");
                } else if ("2".equals(warnData.getWarnlevel())) {
                    warnData.setWarnLevelName("二级");
                } else if ("3".equals(warnData.getWarnlevel())) {
                    warnData.setWarnLevelName("三级");
                }
            }

            //判断报警类型
            List<Map<String, String>> warnTypeDatas = warnTypeDataList.stream().filter(s -> s.get("code").equals(warnType)).collect(Collectors.toList());
            if (StringUtils.isNotEmpty(warnTypeDatas)) {
                warnData.setWarnTypeName(warnTypeDatas.get(0).get("name"));
            }
        }
        //排序
        List<BusWarnWarninfo> retDataList = warnDataInfoList.stream().sorted(Comparator.comparing(BusWarnWarninfo::getWarntime, Comparator.reverseOrder())).collect(Collectors.toList());

        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<BusWarnWarninfo> retDatas = PageUtil.startListPage(retDataList, pageNum, pageSize).getList();
        return retDatas;
    }

    @Override
    public List<BusWarnWarninfo> warnPsMonitoringList(WarnInfoVo vo) {
        if ("1".equals(vo.getDateType())) {//月
            vo.setBeginTime(vo.getBeginTime() + "-01");
            vo.setEndTime(DateUtils.getLastDayOfMonth(DateUtils.dateTime("yyyy-MM", vo.getEndTime())) + " 23:59:59");
        } else if ("2".equals(vo.getDateType())) {//年
            vo.setBeginTime(DateUtils.dateFormat(DateUtils.getFirstDayOfYear(Integer.parseInt(vo.getBeginTime())), "yyyy-MM-dd"));
            vo.setEndTime(DateUtils.dateFormat(DateUtils.getLastDayOfYear(Integer.parseInt(vo.getEndTime())), "yyyy-MM-dd") + " 23:59:59");
        } else {//日
            if (StringUtils.isNotEmpty(vo.getEndTime())) {
                vo.setEndTime(vo.getEndTime() + " 23:59:59");
            }
        }
        //状态
        if (StringUtils.isNotEmpty(vo.getDataType())) {
            vo.setDataTypeList(Arrays.asList(vo.getDataType().split(",")));
        }
        if (StringUtils.isNotEmpty(vo.getItemCode())) {
            vo.setItemCodeList(Arrays.asList(vo.getItemCode().split(",")));
        }
        //异常类型
        if (StringUtils.isNotEmpty(vo.getWarnType())) {
            vo.setWarnTypeList(Arrays.asList(vo.getWarnType().split(",")));
        }

        List<BusWarnWarninfo> warnVideoList = warnDataMapper.warnPsMonitoringList(vo);
        List<Map<String, String>> warnTypeDataList = getWarnTypeDataList("warn_psmonitoring_type");
        for (BusWarnWarninfo warnData : warnVideoList) {
            String warnType = warnData.getWarntype();//报警类型
            String sampleTime = warnData.getWarntime();
            if (StringUtils.isNotEmpty(sampleTime)) {
                warnData.setSampleDate(DateUtils.dateFormat(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", warnData.getWarntime()), "yyyy-MM-dd HH"));
            }
            //判断报警类型
            List<Map<String, String>> warnTypeDatas = warnTypeDataList.stream().filter(s -> s.get("code").equals(warnType)).collect(Collectors.toList());
            if (StringUtils.isNotEmpty(warnTypeDatas)) {
                warnData.setWarnTypeName(warnTypeDatas.get(0).get("name"));
            }

        }

        return warnVideoList;
    }

    @Override
    public List<BusWarnWarninfo> warnVideoList(WarnInfoVo vo) {
        if ("1".equals(vo.getDateType())) {//月
            vo.setBeginTime(vo.getBeginTime() + "-01");
            vo.setEndTime(DateUtils.getLastDayOfMonth(DateUtils.dateTime("yyyy-MM", vo.getEndTime())) + " 23:59:59");
        } else if ("2".equals(vo.getDateType())) {//年
            vo.setBeginTime(DateUtils.dateFormat(DateUtils.getFirstDayOfYear(Integer.parseInt(vo.getBeginTime())), "yyyy-MM-dd"));
            vo.setEndTime(DateUtils.dateFormat(DateUtils.getLastDayOfYear(Integer.parseInt(vo.getEndTime())), "yyyy-MM-dd") + " 23:59:59");
        } else {//日
            if (StringUtils.isNotEmpty(vo.getEndTime())) {
                vo.setEndTime(vo.getEndTime() + " 23:59:59");
            }
        }
        //状态
        if (StringUtils.isNotEmpty(vo.getDataType())) {
            vo.setDataTypeList(Arrays.asList(vo.getDataType().split(",")));
        }
        List<BusWarnWarninfo> warnVideoList = warnDataMapper.warnVideoList(vo);
        List<Map<String, String>> warnTypeDataList = getWarnTypeDataList("warn_video_type");
        for (BusWarnWarninfo warnData : warnVideoList) {
            String warnType = warnData.getWarntype();//报警类型
            String warnTime = warnData.getWarntime();

            //报警级别
            if (StringUtils.isNotEmpty(warnData.getWarnlevel())) {
                if ("1".equals(warnData.getWarnlevel())) {
                    warnData.setWarnLevelName("一级");
                } else if ("2".equals(warnData.getWarnlevel())) {
                    warnData.setWarnLevelName("二级");
                } else if ("3".equals(warnData.getWarnlevel())) {
                    warnData.setWarnLevelName("三级");
                }
            }

            if (StringUtils.isNotEmpty(warnTime)) {
                warnData.setSampleDate(DateUtils.dateFormat(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", warnData.getWarntime()), "yyyy-MM-dd HH"));
            }
            //判断报警类型
            List<Map<String, String>> warnTypeDatas = warnTypeDataList.stream().filter(s -> s.get("code").equals(warnType)).collect(Collectors.toList());
            if (StringUtils.isNotEmpty(warnTypeDatas)) {
                warnData.setWarnTypeName(warnTypeDatas.get(0).get("name"));
            }
            //图片集合
            if (StringUtils.isNotEmpty(warnData.getAttachments())) {
                warnData.setAttachmentsList(Arrays.asList(warnData.getAttachments().split(",")));
            }
        }

        return warnVideoList;
    }

    @Override
    public List<PsCodPollutant> selectPsCodPollutantList(PsCodPollutant psCodPollutant) {
        return warnDataMapper.selectPsCodPollutantList(psCodPollutant);
    }


    public List<Map<String, String>> getWarnTypeData() {
        List<Map<String, String>> mapList = new ArrayList<>();
        SysDictData dictData = new SysDictData();
        dictData.setDictType("warn_type_data");
        dictData.setStatus("0");
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        if (list != null && list.size() > 0) {
            for (SysDictData data : list) {
                String dictLabel = data.getDictLabel();
                Long dictSort = data.getDictSort();
                String dictValue = data.getDictValue();
                Map<String, String> map = new HashMap<>();
                map.put("name", dictLabel);
                map.put("code", dictValue);
                map.put("sort", String.valueOf(dictSort));
                mapList.add(map);
            }

        }
        return mapList;
    }

    public List<Map<String, String>> getWarnTypeDataList(String dictType) {
        List<Map<String, String>> mapList = new ArrayList<>();
        SysDictData dictData = new SysDictData();
        dictData.setDictType(dictType);
        dictData.setStatus("0");
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        if (list != null && list.size() > 0) {
            for (SysDictData data : list) {
                String dictLabel = data.getDictLabel();
                Long dictSort = data.getDictSort();
                String dictValue = data.getDictValue();
                Map<String, String> map = new HashMap<>();
                map.put("name", dictLabel);
                map.put("code", dictValue);
                map.put("sort", String.valueOf(dictSort));
                mapList.add(map);
            }

        }
        return mapList;
    }
}
